package cn.itcast.bos.service.base.Impl;

import cn.itcast.bos.Dao.base.CourierDao;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.service.base.CourierService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.SecondaryTable;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.swing.text.StyledEditorKit;
import java.util.List;
import java.util.Set;

/**
 * Created by Administrator on 2017/7/21.
 */
@Service
@Transactional
public class CourierServiceImpl implements CourierService {
    //注入Dao
    @Autowired

    private CourierDao courierDao;

    @Override
    //添加快递员的方法
    //添加快递员的方法添加一个添加快递员的权限
    @RequiresPermissions("courier_add")
    public void save(Courier courier) {
        courierDao.save(courier);
    }

    @Override
    public Page<Courier> pageQuery(Specification<Courier> specification, PageRequest pageable) {
        return courierDao.findAll(specification,pageable);
    }

    @Override
    public void deltag(String[] idArray) {
        //遍历数组
        for (String ids :idArray){
            //将id转成Integer类型
            Integer id = Integer.parseInt(ids);
            courierDao.delTag(id);

        }
    }

    @Override
    //查询未关联定区的快递员
    public List<Courier> findAllNoassociationFixedArea() {
        Specification<Courier> specification = new Specification<Courier>() {
            @Override
            public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate p1 = cb.isEmpty(root.get("fixedAreas").as(Set.class));
                Predicate p2 = cb.isNull(root.get("deltag").as(Character.class));
                return cb.and(p1,p2);
            }
        };
        return courierDao.findAll(specification);
    }


}
